package andy.ham;

import android.app.Activity;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import java.util.List;

import andy.ham.db.gen.*;
/**
 * Created by admin on 2018/6/20.
 */

public class LoginDiary extends Activity {
    private EditText user;
    private EditText password;
    private Button loginconfirm;
    private Button setzh;

    private String user_text,psw_text;
    private List<Login> list;


    // GreenDAO使用的变量
    private SQLiteDatabase db;
    private andy.ham.db.gen.DaoMaster daoMaster;
    private andy.ham.db.gen.DaoSession daoSession;
    private LoginDao ld;
    private Login login;
    private loginDaoHelper ldh;
    private Long num;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.login);
        initView();
        InitDAO_tag();
        loginListener();

    }
    private void InitDAO_tag() {
        andy.ham.db.gen.DaoMaster.DevOpenHelper helper1 = new andy.ham.db.gen.DaoMaster.DevOpenHelper(this, "login.db", null);
        db = helper1.getWritableDatabase();
        daoMaster = new andy.ham.db.gen.DaoMaster(db);//代表着一个数据库的连接
        daoSession = daoMaster.newSession();//可以让我们使用一些Entity的基本操作和获取Dao操作类
        ld = daoSession.getLoginDao();
        ldh = new loginDaoHelper(ld);
//        diaryTag dx=new diaryTag();
//        dx.setTag("全部");
//        dtd.insert(dx);
//       dtd.deleteAll();
        //   String s="1234"
        //List<Login> list;
        //list.setPwd(s);
        //ld.insert(list);
    }

    private void initView() {
        user = (EditText) findViewById(R.id.username);
        password = (EditText) findViewById(R.id.password);
        loginconfirm = (Button) findViewById(R.id.login);
        setzh=(Button)findViewById(R.id.set);
//        loginconfirm.setTag(1);
//        loginconfirm.setOnClickListener((View.OnClickListener) this);
    }

    public void loginListener(){
        loginconfirm.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v) {
                user_text=user.getText().toString();
                psw_text=password.getText().toString();
                list=ld.loadAll();
                num=ldh.select();
                if(user_text.equals("")||psw_text.equals("")){
                    Toast.makeText(LoginDiary.this, "输入框不得为空", Toast.LENGTH_SHORT).show();
                }
                else{
                    if(num==0) Toast.makeText(LoginDiary.this, "用户名不存在", Toast.LENGTH_SHORT).show();
                    else for(int i=0;i<num;i++){
                        if(user_text.equals(list.get(i).getUsername())){
                            if(psw_text.equals(list.get(i).getPassword())){
                                Intent intent0 = new Intent(LoginDiary.this,LifeDiary.class);
                                startActivity(intent0);
                                break;
                            }
                            else Toast.makeText(LoginDiary.this, "密码错误", Toast.LENGTH_SHORT).show();
                        }
                        else{
                            Toast.makeText(LoginDiary.this, "用户名不存在", Toast.LENGTH_SHORT).show();
                        }
                    }

                }

            }
        });
        setzh.setOnClickListener(new View.OnClickListener(){

            @Override
            public void onClick(View v) {
                Intent intent0 = new Intent(LoginDiary.this,set.class);
                startActivity(intent0);
            }
        });
    }
}
